package com.gg.fmallspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gg.fmallspringboot.entity.UserAddr;
import com.gg.fmallspringboot.mapper.UserAddrMapper;
import com.gg.fmallspringboot.model.Result;
import com.gg.fmallspringboot.service.UserAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 作者:Miki
 * 时间:2021/6/19 14:27
 */
@Service
public class UserAddrServiceImpl implements UserAddrService {

    @Autowired
    private UserAddrMapper userAddrMapper;

    @Override
    public Result<UserAddr> getAllById(Integer userId) {
        QueryWrapper<UserAddr> qw = new QueryWrapper();
        qw.eq("user_id",userId);
        List<UserAddr> userAddrList = userAddrMapper.selectList(qw);
        Result result;
        if (userAddrList==null){
            result = new Result(1,"查询失败");
        }else {
            result = new Result(0,"查询成功",userAddrList);
        }
        return result;
    }

    @Override
    public Result addUserAddr(UserAddr userAddr) {
        int i = userAddrMapper.addUserAddr(userAddr);
        Result result;
        if (i<=0){
            result = new Result(1,"添加失败");
        }else {
            result = new Result(0,"添加成功");
        }
        return result;
    }

    @Override
    public Result updateUserAddr(UserAddr userAddr) {

        int i = userAddrMapper.updateUserAddr(userAddr);
        Result result;
        if (i<=0){
            result = new Result(1,"修改失败");
        }else {
            result = new Result(0,"修改成功");
        }
        return result;
    }

    @Override
    public Result deleteUserAddr(Integer addrId) {
        int i = userAddrMapper.deleteUserAddr(addrId);
        Result result;
        if (i<=0){
            result = new Result(1," 删除失败");
        }else {
            result = new Result(0,"删除成功");
        }
        return result;
    }

    @Override
    public Result<UserAddr> getOne(String addId) {
        QueryWrapper<UserAddr> qw = new QueryWrapper();
        qw.eq("addr_id",addId);
        UserAddr userAddr = userAddrMapper.selectOne(qw);
        Result result;
        if (userAddr==null){
            result = new Result(1,"查询失败");
        }else {
            result = new Result(0,"查询成功",userAddr);
        }
        return result;
    }

    @Override
    public Result<UserAddr> getComAddr(Integer userId) {
        UserAddr userAddr = userAddrMapper.getComaddr(userId);
        Result result;
        if (userAddr==null){
            result = new Result(1,"查询失败");
        }else {
            result = new Result(0,"查询成功",userAddr);
        }
        return result;
    }
}
